package com.lenovo.rc.sil.wangdwa.learning;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class HandlerDemoActivity extends Activity implements OnClickListener {
	
	
    private Handler mHandler = null;
    private EditText mMsg = null;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button btnStart = (Button)findViewById(R.id.BTN_START);
        btnStart.setOnClickListener(this);
        
        mMsg = (EditText)findViewById(R.id.EDIT_MSG);
        
        mHandler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				
				Bundle bundle = msg.getData();
				String sender = bundle.getCharSequence("sender").toString();
				String data = bundle.getString("msg");
				
				addMsg(sender+"|"+data);
				
			}
			
        };
        
    }

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()) {
		case R.id.BTN_START:
			doStart();
			break;
		}
	}

	private void doStart() {
		// TODO Auto-generated method stub
		LocalThread t = new LocalThread(this, mHandler);
		t.start();
	}
	
	private void addMsg(String string) {
		// TODO Auto-generated method stub
		mMsg.append(string + "\n");
		
	}	

}